/***************************************************************************
 *   Copyright (C) 2008 by Łukasz Spintzyk   *
 *   lukasz.spintzyk@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef OSCILLOSCOPE_H
#define OSCILLOSCOPE_H
#include "device_opt.h"
#include <avr/io.h>
#include <inttypes.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>

#include "display.h"
#include "control.h"
#include "adconversion.h"


extern uint32_t status_read;
/*! Initializes all device modules
 */
void initDevice();

extern uint8_t DEV_state; //! State of device
// for example is channel one turned on ?, then bit 0 is 1
#define ISCH1 0
#define ISCH2 1
#define ISHV 2
#define ISTB 3
#define ISADD 4
#define ISSUB 5
#define ISAC 6
#define ISTRG 7


struct channel {
    uint8_t prevVal;
    uint8_t val;
    uint8_t prevTime;
    uint8_t time;
    int32_t sumVal;
    uint16_t sampleCount;
};

typedef struct channel kanal;
kanal pierwszy;






extern upoint8_t cursor1;//! First cursor co-ordinates
extern upoint8_t cursor2;//! Second cursor co-ordinates

extern uint8_t ch1Pos;//!GND level of channel 1 on screen display
extern uint8_t ch2Pos; //!GND level of channel 2 on screen display
extern uint8_t chAddPos;//!GND level of ADD=CH1+CH2 channel on screen display
extern uint8_t chSubPos;//!GND level of SUB=CH1-CH2 channel on screen display
extern uint8_t adjustFreq;//! Value which changes time base period




//kanal pierwszy, drugi, addytywny, r�?nicowy;	//struktury opisuj?ce kana?y informacji


/* Czasy przerwa?: Trzeba przetestowa? przy ilu pr�bkach si? wyrabia oscyloskop
/ *_______________________________________________________________________________________________
/ *Przerwanie timera czasu  | Warto?? licznika | Ilo?? pr�bek na piksel  |  t/div         |LP	|
/ *    a=T=d/TDIV_W	   |	b=a*HZ	      |		c=1     	|  d=10us	 |0	|
/ *    		      	   |		      |		c=2		|  d=20us	 |1	|
/ *    		      	   |		      |		c=5		|  d=50us	 |2	|
/ *    			   |		      |		c=10		|  d=100us	 |3	|
/ *    			   |		      |		c=20		|  d=200us	 |4	|
/ *    		      	   |		      |		c=50		|  d=500us	 |5	|
/ *    		      	   |		      |		c=100		|  d=1ms	 |6	|
/ *    a=T=d/(TDIV_W*2)	   |	b=a*HZ	      |		c=100		|  d=2ms	 |7	|
/ *    a=T=d/(TDIV_W*5)    |		      |				|  d=5ms 	 |8	|
/ *    a=T=d/(TDIV_W*10)   |		      |				|  d=10ms	 |9	|
/ *    a=T=d/(TDIV_W*20    |		      |				|  d=20ms	 |10	|
/ *    a=T=d/(TDIV_W*50)   |		      |				|  d=50ms	 |11	|
/ *    a=T=d/(TDIV_W*100)  |		      |				|  d=100ms	 |12	|
/ *    a=T=d/(TDIV_W*200)  |		      |				|  d=200ms	 |13	|
/ *    a=T=d/(TDIV_W*500)  |		      |				|  d=500ms	 |14	|
/ *    a=T=d/(TDIV_W*1000) |		      |				|  d=1s		 |15	|
/ *    		      	   |		      |				|  		 |
/ *    		      	   |		      |				|  		 |
*/

#define TIME_OPT_NUM 16
#define VOLT_OPT_NUM 8
extern prog_char timeOpt[];

extern prog_uint8_t sampleNum[TIME_OPT_NUM];
extern PGM_P timeText[TIME_OPT_NUM];
extern prog_uint16_t timeValue[];
//indexes for table timeValue which indicates unit of time for example: microseconds - index from 0 to 5 with 5
#define US_BEGIN 0
#define US_END 5
#define MS_BEGIN 6
#define MS_END 14
#define S_BEGIN 15
#define S_END 15

extern prog_uint16_t  voltValue[VOLT_OPT_NUM];
extern PGM_P voltText[VOLT_OPT_NUM];

extern prog_uint8_t stab[];// stan linii kontroluj?cych stopie? wzmocnienia AiB

extern int8_t curTimeOpt;
extern int8_t curVoltOpt;






//deprecated
//void moveCursor(char id, dx);
//void showCursor(char id);
//inline void incTFreq();
//inline void decTFreq();

//#define MV_BEGIN 0
//#define MV_END 5
//#define V_BEGIN 6
//#define V_END 10
//extern char voltUnit[3];
//char timeUnit[3]={'m','V','\0'};
//const uint16_t  voltValue[VOLT_OPT_NUM] PROGMEM={2,20,40,100,200,400, 1,2, 4, 10,..};
//char voltUnit[3]={'m','V','\0'};





#endif
